package com.cskaoyan.service;

import com.cskaoyan.bean.MarketCoupon;
import com.cskaoyan.bean.MarketCouponExample;
import com.cskaoyan.bean.MarketCouponUser;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketCouponMapper;
import com.cskaoyan.mapper.MarketCouponUserMapper;
import com.cskaoyan.util.MyBatisUtil;
import com.cskaoyan.util.StringUtil;
import com.cskaoyan.util.TransactionUtil;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author Ramsey
 * @date 2023年05月30日 9:47
 */
public class CouponServiceImpl implements CouponService {
    @Override
    public CommonData<MarketCoupon> querySelective(String name, Short type, Short status, Integer page, Integer limit, String sort, String order) {
        MarketCouponExample example = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = example.createCriteria();

        if (!StringUtil.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (type != null) {
            criteria.andTypeEqualTo(type);
        }
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtil.isEmpty(sort) && !StringUtil.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);
        MarketCouponMapper couponMapper = MyBatisUtil.getMapper(MarketCouponMapper.class);
        List<MarketCoupon> marketCoupons = couponMapper.selectByExample(example);
        return CommonData.data(marketCoupons);
    }

    @Override
    public void add(MarketCoupon coupon) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketCouponMapper mapper = sqlSession.getMapper(MarketCouponMapper.class);
        mapper.insertSelective(coupon);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public MarketCoupon selectById(Integer id) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketCouponMapper mapper = sqlSession.getMapper(MarketCouponMapper.class);
        MarketCoupon marketCoupon = mapper.selectByPrimaryKey(id);
        return marketCoupon;
    }

    @Override
    public List<MarketCouponUser> selectUserByCouponId(Integer couponId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketCouponUserMapper mapper = sqlSession.getMapper(MarketCouponUserMapper.class);
        List<MarketCouponUser> marketCouponUsers = mapper.selectUserByCouponId(couponId);
        return marketCouponUsers;
    }

    @Override
    public List<MarketCouponUser> selectUserByUserId(Integer userId, Integer couponId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketCouponUserMapper mapper = sqlSession.getMapper(MarketCouponUserMapper.class);
        List<MarketCouponUser> marketCouponUsers = mapper.selectUserByUserId(userId, couponId);
        return marketCouponUsers;
    }

    @Override
    public List<MarketCouponUser> selectUserByStatus(Integer status, Integer couponId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketCouponUserMapper mapper = sqlSession.getMapper(MarketCouponUserMapper.class);
        List<MarketCouponUser> marketCouponUsers = mapper.selectUserByStatus(status, couponId);
        return marketCouponUsers;
    }

    @Override
    public void update(MarketCoupon marketCoupon) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketCouponMapper mapper = sqlSession.getMapper(MarketCouponMapper.class);
        //测试
        int i = mapper.updateByPrimaryKeySelective(marketCoupon);
    }

    @Override
    public void deleteByPhysical(Integer id) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketCouponMapper mapper = sqlSession.getMapper(MarketCouponMapper.class);
        mapper.deleteByPhysical(id);
    }
}
